package com.ecr.hub.front.validator;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

import javax.faces.application.FacesMessage;
import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.validator.ValidatorException;

public abstract class AbstractRegexValidator extends AbstractValidator
		implements IRegexValidator {

	public final void validate(FacesContext arg0, UIComponent arg1, Object arg2)
			throws ValidatorException {

		FacesMessage msg = null;

		try {
			Pattern p = Pattern.compile(getPatternToMatch());
			Matcher m = p.matcher((String) arg2);

			if (!m.matches()) {
				msg = getMessageValidator(getMessageKey(), getAttributsList(),
						getSeverityLevel(), arg0, arg1);
			}
		} catch (Exception e) {
			msg = getMessageErrorInValidator(e);
		}

		if (null != msg)
			throw new ValidatorException(msg);
	}

	// Récupération du pattern pour la chaine à contrôler.
	protected abstract String getPatternToMatch();
}
